package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.TaoBaoAccountInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/6/13.
 */

public class TaoBaoBinding2Fragment extends BaseFragment {

    @BindView(R.id.lv_listView)
    ListView lv_listView;
    @BindView(R.id.bt_addAccount)
    Button bt_addAccount;
    private TaoBaoInfoAdapter taoBaoInfoAdapter;
    private List<TaoBaoAccountInfo> mList = new ArrayList<>();
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_taobao_binding2;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        getBankTaoBao();
    }

    class TaoBaoInfoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_taobao_binding2, null);
                holder = new ViewHolder();
                holder.tv_shopName = (TextView) convertView.findViewById(R.id.tv_shopName);
                holder.tv_auditState = (TextView) convertView.findViewById(R.id.tv_auditState);
                holder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
                holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
                holder.tv_user = (TextView) convertView.findViewById(R.id.tv_user);
                holder.tv_reply = (TextView) convertView.findViewById(R.id.tv_reply);
                holder.tv_replyTitle = (TextView) convertView.findViewById(R.id.tv_replyTitle);
                holder.iv_alter = (ImageView) convertView.findViewById(R.id.iv_alter);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv_shopName.setText("" + mList.get(position).getAccount());
            if (mList.get(position).getAuth() == 0) {
                holder.tv_replyTitle.setVisibility(View.VISIBLE);
                holder.tv_auditState.setText("审核中");
                holder.tv_reply.setVisibility(View.GONE);
                holder.iv_alter.setVisibility(View.VISIBLE);
                holder.tv_replyTitle.setText("为了提高审核工作效率,请内心等待,我们会按顺序尽快审核");
            } else if (mList.get(position).getAuth() == 1) {
                holder.tv_auditState.setText("审核通过");
                holder.tv_reply.setVisibility(View.GONE);
                holder.tv_replyTitle.setVisibility(View.GONE);
                holder.iv_alter.setVisibility(View.GONE);
                holder.tv_auditState.setTextColor(getResources().getColor(R.color.steel_blue));
            } else if (mList.get(position).getAuth() == 2) {
                holder.tv_auditState.setText("审核未通过");
                holder.tv_reply.setVisibility(View.VISIBLE);
                holder.tv_replyTitle.setVisibility(View.VISIBLE);
                holder.iv_alter.setVisibility(View.GONE);
                holder.tv_reply.setText(mList.get(position).getTips() + "");
            }
            holder.tv_address.setText("收货地址:" + mList.get(position).getAddress());
            holder.tv_phone.setText("手机:" + mList.get(position).getRecephone());
            holder.tv_user.setText("联系人:" + mList.get(position).getReceiver());

            return convertView;
        }
    }

    static class ViewHolder {
        public TextView tv_shopName;
        public TextView tv_auditState;
        public TextView tv_address;
        public TextView tv_phone;
        public TextView tv_user;
        public TextView tv_reply;
        public TextView tv_replyTitle;
        public ImageView iv_alter;
    }

    @OnClick({R.id.bt_addAccount})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_addAccount:
                UIHelper.showTaoBaoBingding(mActivity);
                break;
            default:
                break;
        }
    }
    @Subscriber(tag = Constants.TAG_SET_BUYERTAOBAO_SUCCESS)
    private void setBuyerTaoBaoSuccess(boolean prmB) {
        getBankTaoBao();
    }
    private void getBankTaoBao() {
        ApiHttpClient.getBuyerTaoBao(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                                     new JsonCallback<TaoBaoAccountInfo>(TaoBaoAccountInfo.class) {
                                         @Override
                                         public void onBefore(Request request) {
                                             super.onBefore(request);
                                         }

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             toast("网络异常,请稍后重试");
                                         }

                                         @Override
                                         public void onResponse(
                                                 ResponseInfo<TaoBaoAccountInfo> response
                                         ) {
                                             int code = response.getCode();
                                             String message = response.getMessage();
                                             if (code == 1) {
                                                 showWarnDialog(message);
                                                 return;
                                             }
                                             mList = response.getData();
                                             if (null == mList || mList.size() == 0) {
                                                 //toast("获取信息失败");
                                                 return;
                                             }
                                             if (mList.size() >= 3) {
                                                 bt_addAccount.setVisibility(View.GONE);
                                             }
                                             Hawk.put(Constants.TAG_SET_TAOBAO_SUCCESS,mList);
                                             taoBaoInfoAdapter = new TaoBaoInfoAdapter();
                                             lv_listView.setAdapter(taoBaoInfoAdapter);
                                             lv_listView.setOnItemClickListener(
                                                     new AdapterView.OnItemClickListener() {
                                                         @Override
                                                         public void onItemClick(
                                                                 AdapterView<?> parent, View view,
                                                                 int position, long id
                                                         ) {
                                                             Bundle args = new Bundle();
                                                             TaoBaoAccountInfo info = mList.get(
                                                                     position);
                                                             args.putSerializable("info", info);
                                                             UIHelper.showTaoBaoBingdingDetail(
                                                                     mActivity, args);
                                                             finish();
                                                         }

                                                     });
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                         }
                                     }
        );
    }

}
